package cn.neusoft.yuejian307.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import cn.neusoft.yuejian307.Fragment.ortherfragmnet.FragmentMasterAnserQuestion;
import cn.neusoft.yuejian307.Fragment.ortherfragmnet.FragmentMasterCourse;
import cn.neusoft.yuejian307.R;

public class MasterDetail extends AppCompatActivity implements View.OnClickListener{
    MyTitle myTitle;
    private Intent intent;
    private Bundle bundle;
    private int id1,id2;
    private ImageView img;
    private TextView intro,address;

    private LinearLayout tab_intro;
    private LinearLayout tab_course;
    private TextView master_class,master_video,tv1,tv2;
    private android.support.v4.app.Fragment mTab01;
    private android.support.v4.app.Fragment mTab02;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_master_detail);
        intent = getIntent();
        bundle = intent.getExtras();
        init();
        selectEvents();
        setSelect(0);
    }


    private void init(){
        master_class = (TextView) findViewById(R.id.master_class);
        master_video = (TextView) findViewById(R.id.master_video);
        tv1 = (TextView) findViewById(R.id.tv1);
        tv2 = (TextView) findViewById(R.id.tv2);
        tab_intro = (LinearLayout)findViewById(R.id.tab_intro);
        tab_course = (LinearLayout)findViewById(R.id.tab_course);
        myTitle = (MyTitle)findViewById(R.id.tt);
        myTitle.setTitle("大师详情");
        id1 = bundle.getInt("2");
        id2 = bundle.getInt("1");
        img = (ImageView)findViewById(R.id.img);
        Picasso.with(MasterDetail.this).load(bundle.getString("pic")).into(img);
        intro = (TextView)findViewById(R.id.intro);
        intro.setText(bundle.getString("3"));
        address = (TextView)findViewById(R.id.address);
        address.setText(bundle.getString("4"));

    }

    private void selectEvents(){
        tab_intro.setOnClickListener(this);
        tab_course.setOnClickListener(this);
    }
    private void setSelect(int i)
    {
        android.support.v4.app.FragmentManager fm = MasterDetail.this.getSupportFragmentManager();
        android.support.v4.app.FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i) {

            case 0:
                if (mTab01 == null) {
                    mTab01 = new FragmentMasterCourse();
                    transaction.add(R.id.id_content, mTab01);
                } else {
                    transaction.show(mTab01);
                }

                master_class.setTextColor(MasterDetail.this.getResources().getColor(R.color.green));
                tv1.setBackgroundColor(MasterDetail.this.getResources().getColor(R.color.green));

                break;
            case 1:
                if (mTab02 == null) {
                    mTab02 = new FragmentMasterAnserQuestion();
                    transaction.add(R.id.id_content, mTab02);
                } else {
                    transaction.show(mTab02);

                }
                master_video.setTextColor(MasterDetail.this.getResources().getColor(R.color.green));
                tv2.setBackgroundColor(MasterDetail.this.getResources().getColor(R.color.green));

                //tv1.setBackgroundColor(Color.BLUE);
                break;



            default:
                break;
        }
        transaction.commit();
    }

    private void hideFragment(android.support.v4.app.FragmentTransaction transaction)
    {
        if (mTab01 != null)
        {
            transaction.hide(mTab01);
        }
        if (mTab02 != null)
        {
            transaction.hide(mTab02);
        }
    }

    private void resetImgs()
    {
        tv1.setBackgroundColor(MasterDetail.this.getResources().getColor(R.color.bg_Gray));
        tv2.setBackgroundColor(MasterDetail.this.getResources().getColor(R.color.bg_Gray));


        master_class.setTextColor(MasterDetail.this.getResources().getColor(R.color.bg_Gray));
        master_video.setTextColor(MasterDetail.this.getResources().getColor(R.color.bg_Gray));
    }

    @Override
    public void onClick(View view) {
        resetImgs();
        switch (view.getId()){
            case R.id.tab_intro:
                setSelect(0);
                break;
            case R.id.tab_course:
                setSelect(1);
                break;
            case R.id.back:
                onBackPressed();
                break;
            default:
                break;
        }

    }
}
